#import "MainDetegate.h"
#import "DictionaryUtil.h"
#import "HttpRequestParamEnginner.h"
#import "HttpCommucationEngineer.h"
#import "AlertViewEnginner.h"

@implementation CMainDetegate

- (void)applicationDidFinishLaunching:(UIApplication *)application
{
	iRect=[[UIScreen mainScreen] bounds];
	iWindow=[[UIWindow alloc]initWithFrame:iRect];
	[[CHttpRequestParamEnginner GetIntance] InitCHttpRequestParam];

	//CBootUIView *view=[[CBootUIView alloc]initWithFrame:aRect];
	iViewControler=[[CMainViewControler alloc] init];
	iViewControler.iViewType=BootViewType;
	iViewControler.iRect=iRect;
	[iWindow addSubview:iViewControler.view];
	iWindow.backgroundColor=[UIColor clearColor];
	[iWindow makeKeyAndVisible];
	
	iIndicatorView=[[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(150, 260, 20, 20)];
	[iIndicatorView setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhiteLarge];
	[iIndicatorView startAnimating];
	[iWindow addSubview:iIndicatorView];
	[iIndicatorView release];
	iTimer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(onTimer) userInfo:nil repeats:NO];
	[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(HandleSwitchViewMessage:) name:KNotificationSwitchView object:nil];
	[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(HandleBackViewMessage) name:KNotificationBackView object:nil];
	[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(HandleBackFormViewMessage) name:KNotificationBackFormView object:nil];
	
}
-(void)onTimer//切换到主界面
{
	if(iIndicatorView)
	{
		[iIndicatorView stopAnimating];
		[iIndicatorView removeFromSuperview];
		
	}
	if(iViewControler)
	{
		[iViewControler.view removeFromSuperview];//删除启动界面
		[iViewControler release];
	}
	iViewControler=[[CMainViewControler alloc] init];
	iViewControler.iViewType=MainViewType;
	iViewControler.iRect=iRect;
	iNavControler=[[UINavigationController alloc] initWithRootViewController:iViewControler];
	[iWindow insertSubview:iNavControler.view atIndex:0];
	
}
-(void)HandleSwitchViewMessage:(NSNotification *)notify
{
	
	[notify retain];
	[[notify userInfo] retain];
	[self performSelectorOnMainThread:@selector(HandleCustomMessageMainThread:) withObject:notify waitUntilDone:FALSE];
	
}
-(void)HandleBackViewMessage
{
	[iNavControler popViewControllerAnimated:YES];
	iViewControler=(CMainViewControler*)[iNavControler topViewController];
}
-(void)HandleBackFormViewMessage
{
	[iNavControler popToViewController:iFormiViewControler animated:YES];
	iViewControler=(CMainViewControler*)[iNavControler topViewController];
	[iFormiViewControler UpdateView];
}
-(void) HandleCustomMessageMainThread:(NSNotification *)notify
{
	NSDictionary *userInfo;
	NSString *msgName;
	CMainViewControler *currentViewControler;
	userInfo=[notify userInfo];
	msgName=[notify name];
	[notify release];
	int viewType=[CDictionaryUtil GetIntValue:userInfo tagKey:@"viewType"];
	int lucktyType=[CDictionaryUtil GetIntValue:userInfo tagKey:@"lucktyType"];
	if([msgName compare:KNotificationSwitchView]==0)
	{
		switch (viewType)
		{
			case FormViewType://主界面
			{
				currentViewControler=iViewControler;
				iViewControler=[[CMainViewControler alloc] init];
				iViewControler.iViewType=FormViewType;
				iViewControler.iLucktyType=(LucktyType)lucktyType;
				iViewControler.iRect=iRect;
				iFormiViewControler=iViewControler;
				[iNavControler setNavigationBarHidden:NO animated:NO];
				[iNavControler pushViewController:iViewControler animated:NO];
				[iViewControler release];
				[self SwitchView:currentViewControler ComingView:iViewControler];
				break;
			}
			case ProvinceViewType:
			{
				currentViewControler=iViewControler;
				iViewControler=[[CMainViewControler alloc] init];
				iViewControler.iViewType=ProvinceViewType;
				iViewControler.iLucktyType=(LucktyType)lucktyType;
				iViewControler.iRect=iRect;
				[iNavControler setNavigationBarHidden:NO animated:NO];
				[iNavControler pushViewController:iViewControler animated:NO];
				[iViewControler release];
				[self SwitchView:currentViewControler ComingView:iViewControler];
				
				break;
			}
			case CityViewType:
			{
				currentViewControler=iViewControler;
				iViewControler=[[CMainViewControler alloc] init];
				iViewControler.iViewType=CityViewType;
				iViewControler.iLucktyType=(LucktyType)lucktyType;
				iViewControler.iRect=iRect;
				[iNavControler setNavigationBarHidden:NO animated:NO];
				[iNavControler pushViewController:iViewControler animated:NO];
				[iViewControler release];
				[self SwitchView:currentViewControler ComingView:iViewControler];
				break;
			}
			case ZoneViewType:
			{
				
				currentViewControler=iViewControler;
				iViewControler=[[CMainViewControler alloc] init];
				iViewControler.iViewType=ZoneViewType;
				iViewControler.iLucktyType=(LucktyType)lucktyType;
				iViewControler.iRect=iRect;
				[iNavControler setNavigationBarHidden:NO animated:NO];
				[iNavControler pushViewController:iViewControler animated:NO];
				[iViewControler release];
				[self SwitchView:currentViewControler ComingView:iViewControler];
				break;
			}
			case ResultViewType:
			{
				NSString *allUrl=[userInfo objectForKey:@"allUrl"];
				currentViewControler=iViewControler;
				iViewControler=[[CMainViewControler alloc] init];
				iViewControler.iViewType=ResultViewType;
				iViewControler.iLucktyType=(LucktyType)lucktyType;
				iViewControler.iRect=iRect;
				iViewControler.iAllUrl=allUrl;
				[iNavControler setNavigationBarHidden:NO animated:NO];
				[iNavControler pushViewController:iViewControler animated:NO];
				[iViewControler release];
				[self SwitchView:currentViewControler ComingView:iViewControler];
				break;
			}
				
			default:
			break;
		}
	}
	[userInfo release];
}

-(void)SwitchView:(UIViewController*)aCurrentView ComingView:(UIViewController*)aComingView
{
	NSLog(@"%@",@"begin SwitchView");
	[UIView beginAnimations:@"View Flip" context:nil];
	[UIView setAnimationDuration:1.25];
	[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];  
	
	UIViewAnimationTransition transition = UIViewAnimationTransitionFlipFromRight;
	int style = random();
	style %= 4;
	style += 1;
	transition = (UIViewAnimationTransition)style;
	
	[UIView setAnimationTransition:transition forView:iWindow cache:YES];  
	
	[aComingView viewWillAppear:YES];
	[aCurrentView viewWillDisappear:YES];
	
	[aCurrentView.view removeFromSuperview];
	
	[iWindow insertSubview:aComingView.view atIndex:0];  
	
	[aCurrentView viewDidDisappear:YES];
	[aComingView viewDidAppear:YES];
	
	[UIView commitAnimations]; 
	NSLog(@"%@",@"end SwitchView");
}

- (void)applicationWillTerminate:(UIApplication *)application
{
	if([iTimer isValid])
	{
		[iTimer invalidate];
	}
	[self release];
}

-(void)dealloc
{
	[[NSNotificationCenter defaultCenter]removeObserver:self name:KNotificationSwitchView object:nil];
	[[NSNotificationCenter defaultCenter]removeObserver:self name:KNotificationBackView object:nil];
	[[NSNotificationCenter defaultCenter]removeObserver:self name:KNotificationBackFormView object:nil];
	[CHttpRequestParamEnginner Destroy];
	[CHttpCommucationEngineer Destroy];
	[CAlertViewEnginner Destroy];
	[iWindow release];
	[iNavControler release];
	[super dealloc];
}


@end
